package com.sinotrans.gateway.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import com.sinotrans.common.exception.BusinessException;
import com.sinotrans.common.security.AuthFeignTemplate;
import com.sinotrans.common.security.AuthRestTemplate;
import com.sinotrans.common.security.DigestSignature;
import com.sinotrans.logger.aop.LogAspect;

import feign.RequestInterceptor;

@EnableCaching
@Configuration
public class CommonConfig {

	@Bean
	public DigestSignature digestSignature() throws BusinessException {
		return new DigestSignature();
	}

	/*
	 * 切面配置
	 */
	@Aspect
	private class LogAspectConfig extends LogAspect {

		@Around("@annotation(com.sinotrans.logger.api.LogMethod)")
		public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
			return super.logAround(pjp);
		}
	}

	/*
	 * 日志切面
	 */
	@Bean
	public LogAspectConfig logAspectConfig() {
		return new LogAspectConfig();
	}

	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() throws BusinessException {
		return new AuthRestTemplate(digestSignature());
	}

	@Bean
	public RequestInterceptor feignTemplate() throws BusinessException {
		return new AuthFeignTemplate(digestSignature());
	}

}
